//
//  MakeOrderViewController.m
//  YJRenter
//
//  Created by 文彬 on 2018/7/6.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "MakeOrderViewController.h"
#import "OrderInfoTableViewCell.h"
#import "TitleArrowTableViewCell.h"
#import "InputItemTableViewCell.h"
#import "PayOrderViewController.h"

@interface MakeOrderViewController ()<UITableViewDelegate,
    UITableViewDataSource>

@property (strong, nonatomic) UITableView *listTableView;

@end

@implementation MakeOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view
    self.navigationItem.title = @"订单填写";
    
    [self initPageControl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)initPageControl{
    
    [self.view addSubview:self.listTableView];
    [self.listTableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.bottom.equalTo(@-50);
    }];
    
    [self addBottomView];
    
    [self addTableFootView];
}

- (void)addBottomView{
    UIView *bottomView = [[UIView alloc]init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    [bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(@0);
        make.top.equalTo(self.listTableView.bottom).offset(0.5);
    }];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setTitle:@"提交订单" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    confirmBtn.backgroundColor = kMainOriginColor;
    [bottomView addSubview:confirmBtn];
    [confirmBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(@0);
        make.width.equalTo(@100);
    }];
    WeakObj(self)
    [confirmBtn buttonClick:^{
        PayOrderViewController *payOrderController = [[PayOrderViewController alloc]init];
        [selfWeak.navigationController pushViewController:payOrderController animated:YES];
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = @"支付金额";
    [bottomView addSubview:titleLabel];
    [titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.width.equalTo(@100);
        make.height.equalTo(@30);
        make.centerY.equalTo(bottomView.centerY);
    }];
    
    UILabel *moneyLabel = [[UILabel alloc]init];
    moneyLabel.font = [UIFont systemFontOfSize:16];
    moneyLabel.textColor = kMainOriginColor;
    moneyLabel.text = @"￥500.00";
    [bottomView addSubview:moneyLabel];
    [moneyLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@80);
        make.width.equalTo(@200);
        make.height.equalTo(@30);
        make.centerY.equalTo(bottomView.centerY);
    }];
}

- (void)addTableFootView{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 140)];
    self.listTableView.tableFooterView = footView;
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textColor = kSecTitleColor;
    titleLabel.numberOfLines = 0;
    titleLabel.text = @"限时退：取消订单，将收取100%的房费作为违约金支付给房东，入住后若提前退房，将收取100%的剩余房费作为违约金支付给房东\n请根据实际入住人数填写，人数不同房屋报价也有所不同";
    [UILabel changeLineSpaceForLabel:titleLabel WithSpace:4];
    [footView addSubview:titleLabel];
    [titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
        make.top.equalTo(@10);
        make.bottom.equalTo(@0);
    }];
    
    
}
#pragma mark UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section==2?7:1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section==1) {
        return 45;
    }
    else if(indexPath.section==2){
        return 40;
    }
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==1) {
        
        static NSString *iden = @"iden";
        UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:iden];
        if (cell==nil) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        }
        cell.textLabel.text = @"预定无需等待，订单闪电确认";
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textColor = kMainOriginColor;
        return cell;
    }
    else if(indexPath.section==2){
        NSString *iden = @"iden";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
        if (cell==nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        }
        for (UIView *view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.textColor = kTitleColor;
        [cell.contentView addSubview:titleLabel];
        [titleLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@10);
            make.height.equalTo(@30);
            make.width.equalTo(@100);
            make.centerY.equalTo(cell.contentView.centerY);
        }];
        
        UILabel *detailLabel = [[UILabel alloc]init];
        detailLabel.font = [UIFont systemFontOfSize:15];
        detailLabel.textAlignment = NSTextAlignmentRight;
        detailLabel.textColor = kTitleColor;
        [cell.contentView addSubview:detailLabel];
        [detailLabel makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@-10);
            make.height.equalTo(@30);
            make.width.equalTo(@200);
            make.centerY.equalTo(cell.contentView.centerY);
        }];
        
        UIView *lineView = [[UIView alloc]init];
        lineView.backgroundColor = kLineColor;
        [cell.contentView addSubview:lineView];
        [lineView makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(@0);
            make.height.equalTo(@0.5);
        }];
        
        if (indexPath.row==0||indexPath.row==1||indexPath.row==4) {
            
            float width;
            float height;
            NSString *iconStr;
            if (indexPath.row==0) {
                width = 35;
                height = 35;
                iconStr = @"user_setting";
            }
            else if(indexPath.row==1){
                width = 8;
                height = 15;
                 iconStr = @"arrow_detail";
            }
            else {
                width = 25;
                height = 25;
                iconStr = @"user_setting";
            }
            
            UIImageView *imgView = [[UIImageView alloc]init];
            imgView.image= [UIImage imageNamed:iconStr];
            [cell.contentView addSubview:imgView];
            [imgView makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(@-10);
                make.centerY.equalTo(cell.contentView.centerY);
                make.width.equalTo(@(width));
                make.height.equalTo(@(height));
            }];
        }
        
        if (indexPath.row==2||indexPath.row==3) {
            
            [detailLabel updateConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(@-50);
                make.width.equalTo(@70);
                make.centerY.equalTo(cell.contentView.centerY);
                make.height.equalTo(@30);
            }];
            
            UIButton *addBtn= [UIButton buttonWithType:UIButtonTypeCustom];
            [addBtn setBackgroundImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
            [cell.contentView addSubview:addBtn];
            [addBtn makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(@-10);
                make.centerY.equalTo(cell.contentView.centerY);
                make.width.height.equalTo(@25);
            }];
            
            UIButton *minBtn= [UIButton buttonWithType:UIButtonTypeCustom];
            [minBtn setBackgroundImage:[UIImage imageNamed:@"jian"] forState:UIControlStateNormal];
            [cell.contentView addSubview:minBtn];
            [minBtn makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(@-85);
                make.centerY.equalTo(cell.contentView.centerY);
                make.width.height.equalTo(@25);
            }];
            
            if (indexPath.row==2) {
                detailLabel.text = @"1套";
            }
            else{
                detailLabel.text = @"1人";
            }
        }
        
        if (indexPath.row==0) {
            titleLabel.text = @"扫码领福利";
        }
        else if(indexPath.row==1){
            titleLabel.text = @"选择日期";
        }
        else if(indexPath.row==2){
            titleLabel.text = @"房屋套数";
        }
        else if(indexPath.row==3){
            titleLabel.text = @"入住人数";
        }
        else if(indexPath.row==4){
            titleLabel.text = @"入住人";
            
            [detailLabel updateConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(@-100);
                make.width.equalTo(@70);
                make.centerY.equalTo(cell.contentView.centerY);
                make.height.equalTo(@30);
            }];
            detailLabel.text = @"张三";
            
            UIView *lineView = [[UIView alloc]init];
            lineView.backgroundColor = kLineColor;
            [cell.contentView addSubview:lineView];
            [lineView makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(@-70);
                make.width.equalTo(@0.5);
                make.top.equalTo(@5);
                make.bottom.equalTo(@-5);
            }];
            
        }
        else if(indexPath.row==5){
            titleLabel.text = @"手机号+86";
            detailLabel.text = @"1510176633";
        }
        else if(indexPath.row==6){
            titleLabel.text = @"身份证";
            detailLabel.text = @"3534534534534634634";
        }
       
        return cell;
    }
  
    OrderInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderInfoTableViewCell" forIndexPath:indexPath];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg"] placeholderImage:nil];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark getter
- (UITableView*)listTableView{
    if (_listTableView==nil) {
        _listTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.sectionFooterHeight = 0;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.backgroundColor = [UIColor clearColor];
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;        [_listTableView registerNib:[UINib nibWithNibName:@"OrderInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"OrderInfoTableViewCell"];
         _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    
    return _listTableView;
}

@end
